#include <ballade/network.h>

#include "socket_test.h"

namespace Ballade
{
	
	namespace Network
	{
		CPPUNIT_TEST_SUITE_REGISTRATION(SocketTest);
		
		void SocketTest::testConnectSuccess()
		{
			Socket sock("127.0.0.1", 135);
			CPPUNIT_ASSERT(!sock.isConnected());
			sock.connect();
			CPPUNIT_ASSERT(sock.isConnected());
			sock.close();
			CPPUNIT_ASSERT(!sock.isConnected());
			
			sock.connect("127.0.0.1", 135);
			CPPUNIT_ASSERT(sock.isConnected());
			sock.close();
			CPPUNIT_ASSERT(!sock.isConnected());
		}
		
		void SocketTest::testConnectFailed()
		{
			string msg("Connect failed");
			Socket sock("127.0.0.1", 63232);
			try
			{
				sock.connect();
				sock.close();
			}
			catch(SocketError &e)
			{
				CPPUNIT_ASSERT(strncmp(msg.c_str(), e.what(), msg.length()) == 0);
			}
			CPPUNIT_ASSERT(!sock.isConnected());
		}
	}
}